package neusoft.edu.cn.ui_demo.recyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import neusoft.edu.cn.ui_demo.R;

public class RecyclerViewAdapter extends RecyclerView.Adapter<ItemViewHolder> {
    private List<DateItem> dateItemList;
    public Context context;//上下文
    OnItemClickListener mOnItemClickListener;
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener=listener;
    }


    public void setDate(List dateItemList){
        this.dateItemList=dateItemList;
        notifyDataSetChanged();//通知数据集发生变化
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());

        View itemView = layoutInflater.inflate(R.layout.item_layout,viewGroup,false);
        ItemViewHolder viewHolder = new ItemViewHolder(itemView);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, final int i) {
        final DateItem dateItem = dateItemList.get(i);
        itemViewHolder.textView1.setText(dateItem.getText1());
        itemViewHolder.textView2.setText(dateItem.getText2());

        if(mOnItemClickListener!=null)
        {
            itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    System.out.println("aaaa666:"+dateItemList.get(i).getText1());

                    Toast.makeText(view.getContext(),"111"+dateItemList.get(i).getText1(), Toast.LENGTH_SHORT).show();

                    mOnItemClickListener.onItemClick(view,i);
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return dateItemList.size();//返回列表显示的数据个数
    }
}
